package JUC.Locks;

import java.util.concurrent.TimeUnit;

/**
 * 关于锁的8个问题
 * 1.发短信 2.打电话
 * 延迟四秒：1.发短信 2.打电话
 */
public class Test1 {
    public static void main(String[] args) {
        Phone phone=new Phone();
        new Thread(()->{
            phone.sendSms();//锁的问题
        },"A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        new Thread(()->{
            phone.call();
        },"B").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}

class Phone{

    //被synchronized锁的对象是方法的调用者
    //两个方法同一个锁，谁先拿到谁执行
    public synchronized void sendSms(){
        System.out.println("发短信");
    }

    public synchronized void call(){
        System.out.println("打电话");
    }
}